/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package SQLDAL;

import DALFactory.ILogin;
import java.sql.ResultSet;
import Entity.EUser;

/**
 *
 * @author kocio
 */
public class SQLLogin implements ILogin
{
    private EUser user;
    private boolean online;
    /**
     * 用户登录
     * @param userName 用户名
     * @param passWD 密码
     */
    public SQLLogin(String userName,String passWD)
    {
        String sql="SELECT *  FROM [RH].[dbo].[USER] WHERE USERNAME='"+userName+"' and PASSWD='"+passWD+"'";
        SQLStatement sqlstmt=new SQLStatement();
        SQLConnection sc=sqlstmt.openSQLConnection();
        ResultSet rs= sqlstmt.executeQuery(sc,sql);
        try
        {
            if(rs.next())
            {
                while (rs.next())
                {
                    user=new EUser();
                    user.setObjectID("UID"+rs.getInt("UID"));
                    user.setUserID(rs.getInt("UID"));
                    user.setUserName(rs.getString("USERNAME"));
                    user.setPassWD(rs.getString("PASSWD"));
                    user.setEmail(rs.getString("E-mail"));
                    user.setOnline(true);
                    user.setDirty(true);
                }
                this.user=user;
                this.online=true;
            }
            else
            {
                this.online=false;
                rs.close();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            sqlstmt.closeSQLConnection(sc);
        }
    }
    /**
     * 登录的用户
     * @return
     */
    public EUser getUser()
    {
        return user;
    }
    /**
     * 
     * @return 
     */
    public boolean isLogin()
    {
        return online;
    }
}
